// PatientMan.cpp : implementation file
//

#include "stdafx.h"
#include "../Frame/GYWalkAidApp.h"
#include "PatientInfoEditDlg.h"
#include "../Common/GYDataSet.h"
#include "../Common/CommonFun.h"
#include "../Common/LinkInputNode.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


static INPUT_FIELD_INFO s_InputFieldArray[] = 
{
	{NULL,		IBT_NoneType,	FALSE,	FALSE,	_T("MedicalRecordNo"),		ftString,	FALSE,	IDS_PAT_FIELD_MRN,				10,		NULL,		NULL	},
	{NULL,		IBT_NoneType,	FALSE,	TRUE,	_T("Name"),					ftString,	FALSE,	IDS_PAT_FIELD_NAME,				12,		NULL,		NULL	},
	{NULL,		IBT_NoneType,	FALSE,	FALSE,	_T("SerialNo"),				ftNumber,	FALSE,	IDS_PAT_FIELD_SN,				10,		NULL,		NULL	},
	{NULL,		IBT_NoneType,	FALSE,	TRUE,	_T("Gender"),				ftNumber,	FALSE,	IDS_PAT_FIELD_GERDER,			10,		NULL,		NULL	},
	{NULL,		IBT_NoneType,	FALSE,	TRUE,	_T("Stature"),				ftNumber,	TRUE,	IDS_PAT_FIELD_STATURE,			10,		NULL,		NULL	},
	{NULL,		IBT_NoneType,	FALSE,	TRUE,	_T("Weight"),				ftNumber,	TRUE,	IDS_PAT_FIELD_WEIGHT,			10,		NULL,		NULL	},
	{NULL,		IBT_NoneType,	FALSE,	TRUE,	_T("DateOfBirth"),			ftDate,		FALSE,	IDS_PAT_FIELD_DATEOFBIRTH,		10,		CPatInfoEditDlg::DateOfBirthCheck,	CPatInfoEditDlg::DateOfBirthCheck},
	{NULL,		IBT_NoneType,	FALSE,	TRUE,	_T("PhoneNo"),				ftString,	TRUE,	IDS_PAT_FIELD_PHONE_NO,			20,		NULL,		NULL	},
	{NULL,		IBT_NoneType,	TRUE,	TRUE,	_T("FirstClinicDoctor"),	ftString,	FALSE,	IDS_PAT_FIELD_1ST_DOCTOR,		12,		NULL,		NULL	},
	{NULL,		IBT_NoneType,	FALSE,	TRUE,	_T("FirstClinicTime"),		ftDate,		FALSE,	IDS_PAT_FIELD_FIRSTCLINICDATE,	10,		NULL,		NULL	},
	{NULL,		IBT_NoneType,	FALSE,	TRUE,	_T("HomeAddress"),			ftString,	TRUE,	IDS_PAT_FIELD_HOMEADDRESS,		60,		NULL,		NULL	},
	{NULL,		IBT_NoneType,	FALSE,	TRUE,	_T("ClinicalDiagnosis"),	ftString,	FALSE,	IDS_PAT_FIELD_DIAG_RESULT,		60,		NULL,		NULL	},
};

/////////////////////////////////////////////////////////////////////////////
// CPatInfoEditDlg dialog


CPatInfoEditDlg::CPatInfoEditDlg(CWnd* pParent /*=NULL*/) : CDialog(CPatInfoEditDlg::IDD, pParent)
,	m_edtMedicaRecordNo(&s_InputFieldArray[0])
,			 m_edtName(&s_InputFieldArray[1])
,		 m_edtSerialNo(&s_InputFieldArray[2])
,		   m_cmbGender(&s_InputFieldArray[3])
,		  m_edtStature(&s_InputFieldArray[4])
,		   m_edtWeight(&s_InputFieldArray[5])
,	  m_edtDateOfBirth(&s_InputFieldArray[6])
,		  m_edtPhoneNo(&s_InputFieldArray[7])
, m_edt1stClinicDoctor(&s_InputFieldArray[8])
,	m_edt1stClinicDate(&s_InputFieldArray[9])
,	  m_edtHomeAddress(&s_InputFieldArray[10])
,	  m_edtClincalDiag(&s_InputFieldArray[11])
{
	//{{AFX_DATA_INIT(CPatInfoEditDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	
	m_pOriFieldInfo			= NULL;
	m_nOriFieldArraySize	= 0;
}

CPatInfoEditDlg::CPatInfoEditDlg(INPUT_FIELD_INFO* pInputArray, int nArraySize, CWnd* pParent /*=NULL*/) : CDialog(CPatInfoEditDlg::IDD, pParent)
,	m_edtMedicaRecordNo(&s_InputFieldArray[0])
,			 m_edtName(&s_InputFieldArray[1])
,		 m_edtSerialNo(&s_InputFieldArray[2])
,		   m_cmbGender(&s_InputFieldArray[3])
,		  m_edtStature(&s_InputFieldArray[4])
,		   m_edtWeight(&s_InputFieldArray[5])
,	  m_edtDateOfBirth(&s_InputFieldArray[6])
,		  m_edtPhoneNo(&s_InputFieldArray[7])
, m_edt1stClinicDoctor(&s_InputFieldArray[8])
,	m_edt1stClinicDate(&s_InputFieldArray[9])
,	  m_edtHomeAddress(&s_InputFieldArray[10])
,	  m_edtClincalDiag(&s_InputFieldArray[11])
{
	m_pOriFieldInfo			= pInputArray;
	m_nOriFieldArraySize	= nArraySize;
}


void CPatInfoEditDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CPatInfoEditDlg)
	DDX_Control(pDX, IDC_MODI_COMBO_GENDER, m_cmbGender);
	DDX_Control(pDX, IDC_EDIT_MODI_CLIN_DIAG, m_edtClincalDiag);
	DDX_Control(pDX, IDC_EDIT_MODI_HOME_ADDR, m_edtHomeAddress);
	DDX_Control(pDX, IDC_EDIT_MODI_1ST_CLINTIME, m_edt1stClinicDate);
	DDX_Control(pDX, IDC_EDIT_MODI_1ST_CLINDOC, m_edt1stClinicDoctor);
	DDX_Control(pDX, IDC_EDIT_MODI_DOB, m_edtDateOfBirth);
	DDX_Control(pDX, IDC_EDIT_MODI_PHONENO, m_edtPhoneNo);
	DDX_Control(pDX, IDC_EDIT_MODI_WEIGHT, m_edtWeight);
	DDX_Control(pDX, IDC_EDIT_MODI_STATURE, m_edtStature);
	DDX_Control(pDX, IDC_EDIT_MODI_SN, m_edtSerialNo);
	DDX_Control(pDX, IDC_EDIT_MODI_NAME, m_edtName);
	DDX_Control(pDX, IDC_EDIT_MODI_MRN, m_edtMedicaRecordNo);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CPatInfoEditDlg, CDialog)
	//{{AFX_MSG_MAP(CPatInfoEditDlg)
	ON_BN_CLICKED(IDB_PATEDIT_SAVE, OnPateditSave)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPatInfoEditDlg message handlers

BOOL CPatInfoEditDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	for(int i = 0; i < m_nOriFieldArraySize; i++)
	{
		switch(m_pOriFieldInfo[i].dwCtrlType)
		{
		case IBT_EditBox:
			CopyValue((CLinkEdit*)s_InputFieldArray[i].pLinkCtrl, (CLinkEdit*)m_pOriFieldInfo[i].pLinkCtrl);
			break;
			
		case IBT_EditDate:
			CopyValue((CLinkDateEdit*)s_InputFieldArray[i].pLinkCtrl, (CLinkDateEdit*)m_pOriFieldInfo[i].pLinkCtrl);
			break;
			
		case IBT_ComboBox:
			CopyValue((CLinkComboBox*)s_InputFieldArray[i].pLinkCtrl, (CLinkComboBox*)m_pOriFieldInfo[i].pLinkCtrl);
			break;
		}
	}

	m_edtName.SetFocus();

	CLinkInputNode::EnableCheckValid(TRUE, IBT_AllType, this);

	return FALSE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CPatInfoEditDlg::OnPateditSave() 
{
	CString strPartSQL;
	CString strSQL = _T("UPDATE PatientInfo SET ");
	for(int i = 0; i < ARRAY_ELE_COUNT(s_InputFieldArray); i++)
	{
		if(s_InputFieldArray[i].dwCtrlType == IBT_EditBox)
		{
			CLinkEdit* pModiEdit = (CLinkEdit*)s_InputFieldArray[i].pLinkCtrl;

			if(s_InputFieldArray[i].dwFieldType == ftNumber)
				strPartSQL.Format(_T("%s=%s,"), s_InputFieldArray[i].szFieldName, pModiEdit->GetControlData());
			else
			{
				//  'first clinic doctor' is read only
				if(_tcsicmp(s_InputFieldArray[i].szFieldName, _T("FirstClinicDoctor")) != 0)
					strPartSQL.Format(_T("%s='%s',"), s_InputFieldArray[i].szFieldName, pModiEdit->GetControlData());
			}

			strSQL += strPartSQL;
		}
		
		if(s_InputFieldArray[i].dwCtrlType == IBT_EditDate)
		{
			CLinkDateEdit* pDate = (CLinkDateEdit*)s_InputFieldArray[i].pLinkCtrl;

			strPartSQL.Format(_T("%s='%s',"), s_InputFieldArray[i].szFieldName, pDate->GetControlData());
			strSQL += strPartSQL;
		}
		
		if(s_InputFieldArray[i].dwCtrlType == IBT_ComboBox)
		{
			CLinkComboBox* pCombo = (CLinkComboBox*)s_InputFieldArray[i].pLinkCtrl;
			
			strPartSQL.Format(_T("%s=%s,"), s_InputFieldArray[i].szFieldName, pCombo->GetControlData());
			strSQL += strPartSQL;
		}
	}
	
	strSQL.TrimRight(_T(","));

	strPartSQL.Format(_T(" WHERE MedicalRecordNo='%s'"), m_edtMedicaRecordNo.GetControlData());
	strSQL += strPartSQL;
	
	CGYDataSet tblPatInfo;
	tblPatInfo.ExecuteSQL(strSQL);

	CopyToOri();

	EndDialog(IDOK);
}

BOOL CPatInfoEditDlg::CopyToOri()
{
	for(int i = 0; i < m_nOriFieldArraySize; i++)
	{
		switch(m_pOriFieldInfo[i].dwCtrlType)
		{
		case IBT_EditBox:
			CopyValue((CLinkEdit*)m_pOriFieldInfo[i].pLinkCtrl, (CLinkEdit*)s_InputFieldArray[i].pLinkCtrl);
			break;

		case IBT_EditDate:
			CopyValue((CLinkDateEdit*)m_pOriFieldInfo[i].pLinkCtrl, (CLinkDateEdit*)s_InputFieldArray[i].pLinkCtrl);
			break;

		case IBT_ComboBox:
			CopyValue((CLinkComboBox*)m_pOriFieldInfo[i].pLinkCtrl, (CLinkComboBox*)s_InputFieldArray[i].pLinkCtrl);
			break;
		}
	}

	return TRUE;
}

template <class T> BOOL CPatInfoEditDlg::CopyValue(T* pTo, T* pFrom)
{
	if(pFrom == NULL || pTo == NULL)
		return FALSE;

	return pTo->CopyFrom(pFrom);
}

BOOL CPatInfoEditDlg::DateOfBirthCheck(LPVOID pParam)
{
	if(pParam)
	{
		INPUT_FIELD_INFO* pFieldInfo = (INPUT_FIELD_INFO*)pParam;
		CLinkDateEdit* pDateOfBirth = (CLinkDateEdit*)pFieldInfo->pLinkCtrl;
		if(pDateOfBirth == NULL)
			return FALSE;

		CString strDateOfBirth = pDateOfBirth->GetControlData();
		
		int nYear = _ttoi(strDateOfBirth.Left(4));
		int nMonth = _ttoi(strDateOfBirth.Mid(5, 2));
		int nDay = _ttoi(strDateOfBirth.Right(2));
		
		if(!IsValidDateTime(nYear, nMonth, nDay, 0, 0, 0))
		{
			GYMsgBox(IDS_PAT_INVALID_DATE, MB_OK|MB_ICONINFORMATION);
			return FALSE;
		}
		
		CPatInfoEditDlg* pParent = (CPatInfoEditDlg*)pDateOfBirth->GetParent();
		if(pParent)
		{
			CString str1stDiagDay = pParent->m_edt1stClinicDate.GetControlData();
			if(strDateOfBirth >= str1stDiagDay)
			{
				GYMsgBox(IDS_PAT_BIRTH_LESS_1STDIAGDAY, MB_OK|MB_ICONINFORMATION);
				return FALSE;
			}

			return TRUE;
		}
	}
	
	return FALSE;
}
